package com.vvv.shortlink.admin.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vvv.shortlink.common.convention.result.Result;
import com.vvv.shortlink.admin.remote.ShortLinkProjectClient;
import com.vvv.shortlink.admin.remote.dto.req.LinkBatchCreateReqDTO;
import com.vvv.shortlink.admin.remote.dto.req.LinkCreateReqDTO;
import com.vvv.shortlink.admin.remote.dto.req.LinkPageReqDTO;
import com.vvv.shortlink.admin.remote.dto.req.LinkUpdateReqDTO;
import com.vvv.shortlink.admin.remote.dto.resp.LinkBaseInfoRespDTO;
import com.vvv.shortlink.admin.remote.dto.resp.LinkBatchCreateRespDTO;
import com.vvv.shortlink.admin.remote.dto.resp.LinkCreateRespDTO;
import com.vvv.shortlink.admin.remote.dto.resp.LinkPageRespDTO;
import com.vvv.shortlink.common.toolkit.EasyExcelWebUtil;
import com.vvv.shortlink.common.toolkit.UserContext;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author vvv
 * @date 2024/2/2 10 24
 * discription
 */
@RestController
@RequiredArgsConstructor
public class AdminLinkController {

    private final ShortLinkProjectClient shortLinkProjectClient;

    @GetMapping("/api/short-link/admin/v1/page")
    public Result<Page<LinkPageRespDTO>> pageShortLink(LinkPageReqDTO requestParam) {
        Long userId = UserContext.getUserId();
        requestParam.setUserId(userId);
        return shortLinkProjectClient.pageShortLink(requestParam);
    }

    @PostMapping("/api/short-link/admin/v1/create")
    public Result<LinkCreateRespDTO> createShortLink(@RequestBody LinkCreateReqDTO requestParam) {
        Long userId = UserContext.getUserId();
        requestParam.setUserId(userId);
        return shortLinkProjectClient.createShortLink(requestParam);
    }

    /**
     * 批量创建短链接
     */
    @PostMapping("/api/short-link/admin/v1/create/batch")
    public void batchCreateShortLink(@RequestBody LinkBatchCreateReqDTO requestParam, HttpServletResponse response) {
        Long userId = UserContext.getUserId();
        requestParam.setUserId(userId);
        Result<LinkBatchCreateRespDTO> result = shortLinkProjectClient.batchCreateShortLink(requestParam);
        if (result.isSuccess()) {
            List<LinkBaseInfoRespDTO> baseLinkInfos = result.getData().getBaseLinkInfos();
            EasyExcelWebUtil.write(response, "批量创建短链接-SaaS短链接系统", LinkBaseInfoRespDTO.class, baseLinkInfos);
        }
    }

    @PostMapping("/api/short-link/admin/v1/update")
    public Result<Void> updateShortLink(@RequestBody LinkUpdateReqDTO requestParam) {
        Long userId = UserContext.getUserId();
        requestParam.setUserId(userId);
        return shortLinkProjectClient.updateShortLink(requestParam);
    }
}
